package ae.pcfc.cldos.online.web.util;

import java.util.ArrayList;
import java.util.List;

import org.springframework.ldap.core.DirContextOperations;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.ldap.userdetails.LdapAuthoritiesPopulator;

import ae.pcfc.cldos.online.common.delegate.UtilityDelegate;

public class DBAuthoritiesPopulator implements LdapAuthoritiesPopulator {

	UtilityDelegate delegate;

//	@Resource(name = "utilityDelegate")
//	public void setDelegate(UtilityDelegate delegate) {
//		this.delegate = delegate;
//	}

	public List<GrantedAuthority> getGrantedAuthorities(DirContextOperations userData, String username) {

		List<String> rolesList = delegate.getUserSecurityRole(username);
		List<GrantedAuthority> grantedAuthorityList = new ArrayList<GrantedAuthority>();
		if (rolesList != null) {
			for (String role : rolesList) {
				grantedAuthorityList.add(new GrantedAuthorityImpl(role));
			}
		}
		return grantedAuthorityList;
	}
}
